package org.zhangxiao.paladin2.common.util;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeUtils {

    private static final String defaultFormat = "yyyy-MM-dd HH:mm:ss";

    public static String toStr(LocalDateTime dateTime, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return dateTime.format(formatter);
    }

    public static String toStr(LocalDateTime date) {
        return toStr(date, defaultFormat);
    }

    public static LocalDateTime toDateTime(String dateStr, String dateFormat) throws ParseException {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateFormat);
        return LocalDateTime.parse(dateStr, formatter);
    }

    public static LocalDateTime toDateTime(String dateStr) throws ParseException {
        return toDateTime(dateStr, defaultFormat);
    }

}
